// ignore_for_file: unnecessary_null_comparison

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../app_views.dart';

class ToolsView extends GetView<ToolsLogic> {
  const ToolsView({super.key});

  void _showComingSoon(String feature) {
    Get.dialog(
      AlertDialog(
        backgroundColor: AppColors.backgroundCard,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.circular(24),
        ),
        title: Text(
          '功能建设中',
          style: GoogleFonts.notoSansSc(
            fontSize: 20,
            fontWeight: FontWeight.w700,
            color: AppColors.textPrimary,
          ),
        ),
        content: Text(
          '$feature 即将上线，敬请期待。',
          style: GoogleFonts.notoSansSc(
            fontSize: 16,
            color: AppColors.textSecondary,
          ),
        ),
        actions: [
          TextButton(
            onPressed: Get.back,
            style: TextButton.styleFrom(
              backgroundColor: AppColors.primary.withOpacity(0.1),
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(16),
              ),
            ),
            child: Text(
              '知道了',
              style: GoogleFonts.notoSansSc(
                fontSize: 16,
                fontWeight: FontWeight.w600,
                color: AppColors.primary,
              ),
            ),
          ),
        ],
      ),
    );
  }

  // ============今日老黄历开始===========
  IconData _getIconForTool(String tool) {
    switch (tool) {
      case '今日老黄历':
        return Icons.calendar_today_rounded;
      case '生肖运程':
        return Icons.pets_rounded;
      case '配对测算':
        return Icons.favorite_border_rounded;
      case '起名改名':
        return Icons.edit_rounded;
      case '黄历查询':
        return Icons.calendar_month_rounded;
      case '风水指南':
        return Icons.home_rounded;
      default:
        return Icons.apps_rounded;
    }
  }
  // ============今日老黄历结束===========

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColors.background,
      appBar: AppBar(
        backgroundColor: AppColors.backgroundCard,
        elevation: 0,
        title: Text(
          '工具',
          style: GoogleFonts.notoSansSc(
            fontSize: 20,
            fontWeight: FontWeight.w700,
            color: AppColors.textPrimary,
          ),
        ),
        centerTitle: true,
      ),
      body: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter,
            colors: [
              AppColors.background,
              AppColors.backgroundLight,
            ],
          ),
        ),
        child: Obx(() {
          final tools = controller.state.value.tools;

          if (controller.state.value.isLoading) {
            return Center(
              child: CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(AppColors.primary),
                strokeWidth: 3,
              ),
            );
          }

          return SingleChildScrollView(
            padding: const EdgeInsets.all(24),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // 页面标题和描述 - Page title and description
                Container(
                  padding: const EdgeInsets.all(24),
                  decoration: BoxDecoration(
                    gradient: LinearGradient(
                      begin: Alignment.topLeft,
                      end: Alignment.bottomRight,
                      colors: AppColors.gradientWarm,
                    ),
                    borderRadius: BorderRadius.circular(28),
                    boxShadow: [
                      BoxShadow(
                        color: AppColors.primary.withOpacity(0.3),
                        blurRadius: 20,
                        offset: const Offset(0, 8),
                      ),
                    ],
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          Container(
                            padding: const EdgeInsets.all(12),
                            decoration: BoxDecoration(
                              color: AppColors.textOnPrimary.withOpacity(0.2),
                              borderRadius: BorderRadius.circular(16),
                            ),
                            child: const Icon(
                              Icons.calendar_today_rounded,
                              color: AppColors.textOnPrimary,
                              size: 28,
                            ),
                          ),
                          const SizedBox(width: 16),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                // ============今日老黄历开始===========
                                Text(
                                  '今日老黄历',
                                  style: GoogleFonts.notoSansSc(
                                    fontSize: 28,
                                    fontWeight: FontWeight.w800,
                                    color: AppColors.textOnPrimary,
                                  ),
                                ),
                                const SizedBox(height: 8),
                                // 显示今日老黄历内容 - Display today's lunar calendar content
                                _buildLunarCalendarContent(),
                                // ============今日老黄历结束===========
                              ],
                            ),
                          ),
                        ],
                      ),
                    ],
                  ),
                ),
                const SizedBox(height: 32),

                // 工具网格 - Tools grid
                GridView.builder(
                  shrinkWrap: true,
                  physics: const NeverScrollableScrollPhysics(),
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    childAspectRatio: 1.0,
                    crossAxisSpacing: 20,
                    mainAxisSpacing: 20,
                  ),
                  itemCount: tools.length,
                  itemBuilder: (context, index) {
                    final item = tools[index];
                    return _buildToolCard(item, index);
                  },
                ),
              ],
            ),
          );
        }),
      ),
    );
  }

  /// 构建今日老黄历内容 - Build today's lunar calendar content
  Widget _buildLunarCalendarContent() {
    return Obx(() {
      final lunarInfo = controller.state.value.lunarInfo;
      
      if (lunarInfo == null) {
        return Text(
          '正在加载...',
          style: GoogleFonts.notoSansSc(
            fontSize: 14,
            color: AppColors.textOnPrimary.withOpacity(0.8),
          ),
        );
      }

      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            '${lunarInfo['lunar_date'] ?? ''}',
            style: GoogleFonts.notoSansSc(
              fontSize: 16,
              fontWeight: FontWeight.w600,
              color: AppColors.textOnPrimary,
            ),
          ),
          const SizedBox(height: 4),
          Text(
            '${lunarInfo['ganzhi'] ?? ''} ${lunarInfo['zodiac'] ?? ''}年',
            style: GoogleFonts.notoSansSc(
              fontSize: 14,
              color: AppColors.textOnPrimary.withOpacity(0.9),
            ),
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      '宜：${(lunarInfo['yi'] as List?)?.take(2).join('、') ?? ''}',
                      style: GoogleFonts.notoSansSc(
                        fontSize: 12,
                        color: AppColors.textOnPrimary.withOpacity(0.8),
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 2),
                    Text(
                      '忌：${(lunarInfo['ji'] as List?)?.take(2).join('、') ?? ''}',
                      style: GoogleFonts.notoSansSc(
                        fontSize: 12,
                        color: AppColors.textOnPrimary.withOpacity(0.8),
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      );
    });
  }

  /// 构建工具卡片 - Build tool card
  Widget _buildToolCard(String item, int index) {
    final colors = [
      AppColors.gradientCool,
      AppColors.gradientWarm,
      AppColors.gradientPurple,
      AppColors.gradientGreen,
    ];
    
    final gradientColors = colors[index % colors.length];

    return InkWell(
      onTap: () => _showComingSoon(item),
      borderRadius: BorderRadius.circular(24),
      child: Container(
        decoration: BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
            colors: gradientColors,
          ),
          borderRadius: BorderRadius.circular(24),
          border: Border.all(
            color: AppColors.primary.withOpacity(0.2),
            width: 1,
          ),
          boxShadow: [
            BoxShadow(
              color: AppColors.shadow,
              blurRadius: 16,
              offset: const Offset(0, 8),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: 64,
              height: 64,
              decoration: BoxDecoration(
                color: AppColors.textOnPrimary.withOpacity(0.2),
                borderRadius: BorderRadius.circular(20),
                border: Border.all(
                  color: AppColors.textOnPrimary.withOpacity(0.3),
                  width: 1,
                ),
              ),
              child: Icon(
                _getIconForTool(item),
                size: 32,
                color: AppColors.textOnPrimary,
              ),
            ),
            const SizedBox(height: 16),
            Text(
              item,
              textAlign: TextAlign.center,
              style: GoogleFonts.notoSansSc(
                fontSize: 14,
                fontWeight: FontWeight.w600,
                color: AppColors.textOnPrimary,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
